﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SelfService.Model;
using WalkingTec.Mvvm.Core;

namespace SelfService.Tasks;

[Display(Name = "清理过期日志")]
public class ClearLogTask : BaseTask
{
    public ClearLogTask(WTMContext context) : base(context)
    {
    }

    public override async Task<bool> Process()
    {
        this.Logger("开始清理过期日志", ActionLogTypesEnum.Job);
        try
        {
            var dc = Context.DC;
            await dc.Database.ExecuteSqlRawAsync("DELETE FROM ActionLogs WHERE ActionTime < DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND LogType != 1");
            await dc.Database.ExecuteSqlRawAsync("DELETE FROM ActionLogs WHERE ActionUrl = '/api/SysClientTask/GetTask/Client'");
            return true;
        }
        catch (Exception e)
        {
            this.Logger("清理日志异常:" + e.Message, ActionLogTypesEnum.Exception);
            return false;
        }
    }
}